﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
using Utils;
namespace UI.ModSalary
{
    public partial class SalaryYearEnd : System.Web.UI.Page
    {
        SalaryYearBiz salaryYearBiz = new SalaryYearBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                new DepartmentBiz().InitDropDownList(ddlDeptID, "0");
                btnAdd.Attributes.Add("onclick", "javascript:ShowLink('SalaryYearAdd.aspx',800,550);");
                defDicBiz.InitXMLDropDownList(ddlYear, "YearList.xml", (System.DateTime.Now.Year - 1).ToString());
                //年终奖一般是 下一年 发上一年的
                defDicBiz.InitXMLDropDownList(ddlYear1, "YearList.xml", (System.DateTime.Now.Year - 1).ToString());
                defDicBiz.InitXMLDropDownList(ddlYear2, "YearList.xml", System.DateTime.Now.Year.ToString());
                defDicBiz.InitXMLDropDownList(ddlMonth2, "MonthList.xml", System.DateTime.Now.AddMonths(-1).Month.ToString());
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID2, "1");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strWord = txtSerWord.Text.Trim();
            string strYear = ddlYear.SelectedValue;
            string deptID = ddlDeptID.SelectedValue;
            string setsID = ddlSetsID.SelectedValue;
            DataTable dt = salaryYearBiz.GetList(strWord, strYear, deptID, setsID);
            gvList.DataSource = dt;
            gvList.DataBind();

        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[2].Text = "合计";
                e.Row.Cells[3].Text = gvList.Rows.Count + "人";
                //int cols = gvList.HeaderRow.Cells.Count - 1;
                for (int i = 10; i <= 18; i++)
                {                   
                    e.Row.Cells[i-1].Text = Utils.ToolUtility.ReturnTotal(gvList, i).ToString();
                }
            }
        }

        protected void btnSet_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear2.SelectedValue;
            string strMonth = ddlMonth2.SelectedValue;
            // int iMonth = Int32.Parse(strMonth) + 1;
            string setsID = ddlSetsID2.SelectedValue;
            //if (!salaryTableBiz.IsMonthEnd(strYear, iMonth.ToString(), setsID))
            //{
            //    JSUtility.Alert(this, "该月已经结账，不能进行任何修改了!");
            //    return;//已经结账不能再次修改了
            //}
            //获取年终奖 在帐套中的字段
            if (salaryYearBiz.CopyYearToSalTable(ddlYear1.SelectedValue, strYear, strMonth, setsID))
            {
               new  SalaryTableBiz().ReCau(setsID, "", strYear, strMonth);
                JSUtility.Alert(this, "成功增加年终奖!");
            }
            else
                JSUtility.Alert(this, "增加年终奖失败!");
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear.SelectedValue;
            string setsID = ddlSetsID.SelectedValue;
            if (setsID=="0")
            {
                JSUtility.Alert(this, "请选择帐套!");
                return;            
            }
            if (salaryYearBiz.IsExist(strYear, setsID))
            {
                JSUtility.Alert(this, "已经存在该年的年终奖，不能再生成!");
                return;
            }
            if (salaryYearBiz.CreateYear(setsID, strYear))
            {
                JSUtility.Alert(this, "成功生成年终奖!");
            }
            else
                JSUtility.Alert(this, "生成失败!");
        }
        //导出EXCEL
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            string strWord = txtSerWord.Text.Trim();
            string strYear = ddlYear.SelectedValue;
            string deptID = ddlDeptID.SelectedValue;
            string setsID = ddlSetsID.SelectedValue;
            DataTable dt = salaryYearBiz.GetOut(strWord, strYear, deptID, setsID);
            ExcelUtility.ExportDsToXls(this, dt, "SalaryYearEnd.xls");
        }
        //删除年终奖金
        protected void btnDel_Click(object sender, EventArgs e)
        {
            string strWord = txtSerWord.Text.Trim();
            string strYear = ddlYear.SelectedValue;         
            string setsID = ddlSetsID.SelectedValue;
            if(salaryYearBiz.Delete(strYear, strWord, setsID))
                JSUtility.Alert(this, "成功删除选定年月的年终奖!");
            else
                JSUtility.Alert(this, "删除失败!");
        }
        //发放 更改为不可删除的状态
        protected void btnApply_Click(object sender, EventArgs e)
        {
             string strYear = ddlYear.SelectedValue;         
            string setsID = ddlSetsID.SelectedValue;
            if(salaryYearBiz.IsApply(strYear,setsID))
                 JSUtility.Alert(this, "发放完成!");
            else
                JSUtility.Alert(this, "发放失败!");
        }
    }
}